package com.castle.fortress.admin.study.course.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.study.course.dto.StudyCoursewareDto;
import com.castle.fortress.admin.study.course.entity.StudyCoursewareEntity;
import com.castle.fortress.admin.study.course.service.StudyCoursewareService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课件管理 api 控制器
 *
 * @author castle
 * @since 2023-01-30
 */
@Api(tags="课件管理api管理控制器")
@RestController
@RequestMapping("/api/study/course/studyCourseware")
public class ApiStudyCoursewareController {
	@Autowired
	private StudyCoursewareService studyCoursewareService;


	/**
	 * 课件管理的分页展示
	 * @param studyCoursewareDto 课件管理实体类
	 * @param currentPage 当前页
	 * @param size  每页记录数
	 * @return
	 */
    @CastleLog(operLocation="课件管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课件管理分页展示")
	@GetMapping("/page")
	public RespBody<IPage<StudyCoursewareDto>> pageStudyCourseware(StudyCoursewareDto studyCoursewareDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
		Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<StudyCoursewareDto> page = new Page(pageIndex, pageSize);

		IPage<StudyCoursewareDto> pages = studyCoursewareService.pageStudyCourseware(page, studyCoursewareDto);
		return RespBody.data(pages);
	}

	/**
	 * 课件管理保存
	 * @param studyCoursewareDto 课件管理实体类
	 * @return
	 */
    @CastleLog(operLocation="课件管理",operType = OperationTypeEnum.INSERT)
	@ApiOperation("课件管理保存")
	@PostMapping("/save")
	public RespBody<String> saveStudyCourseware(@RequestBody StudyCoursewareDto studyCoursewareDto){
		if(studyCoursewareDto == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			StudyCoursewareEntity studyCoursewareEntity = ConvertUtil.transformObj(studyCoursewareDto,StudyCoursewareEntity.class);
		if(studyCoursewareService.save(studyCoursewareEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 课件管理编辑
	 * @param studyCoursewareDto 课件管理实体类
	 * @return
	 */
    @CastleLog(operLocation="课件管理",operType = OperationTypeEnum.UPDATE)
	@ApiOperation("课件管理编辑")
	@PostMapping("/edit")
	public RespBody<String> updateStudyCourseware(@RequestBody StudyCoursewareDto studyCoursewareDto){
		if(studyCoursewareDto == null || studyCoursewareDto.getId() == null || studyCoursewareDto.getId().equals(0L)){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			StudyCoursewareEntity studyCoursewareEntity = ConvertUtil.transformObj(studyCoursewareDto,StudyCoursewareEntity.class);
		if(studyCoursewareService.updateById(studyCoursewareEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 课件管理删除
	 * @param ids 课件管理id集合
	 * @return
	 */
    @CastleLog(operLocation="课件管理",operType = OperationTypeEnum.DELETE)
	@ApiOperation("课件管理删除")
	@PostMapping("/delete")
	public RespBody<String> deleteStudyCourseware(@RequestBody List<Long> ids){
		if(ids == null || ids.isEmpty()){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(studyCoursewareService.removeByIds(ids)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 课件管理详情
	 * @param id 课件管理id
	 * @return
	 */
    @CastleLog(operLocation="课件管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课件管理详情")
	@GetMapping("/info")
	public RespBody<StudyCoursewareDto> infoStudyCourseware(@RequestParam Long id){
		MemberDto member = WebUtil.currentMember();
		if(member==null){
			throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
		}
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		StudyCoursewareDto studyCoursewareDto =  studyCoursewareService.getByIdExtends(id);
		return RespBody.data(studyCoursewareDto);
	}


}
